#ifndef _MYCAR_H_
#define _MYCAR_H_

//wifi账户
char ssid[] = "liuzewen1";
char pswd[] = "17609245102liu";

//定义超声波引脚
#define TRIG_PIN 19
#define ECHO_PIN 18
//超声波测到的距离
float car_distance = 0;

//定义电源电压检测引脚
#define POWER_ADC_PIN 15
#define power_adc_read analogRead(POWER_ADC_PIN)

//LED引脚定义
#define LED_PIN   2 //gpio2
#define LED_OFF   digitalWrite(LED_PIN, LOW)//关灯
#define LED_ON    digitalWrite(LED_PIN, HIGH)//开灯
#define LED_PWM   digitalWrite(LED_PIN, !digitalRead(LED_PIN))//灯闪烁

//蜂鸣器引脚
#define BEEP_PIN  17
#define BEEP_ON   digitalWrite(BEEP_PIN, HIGH)//蜂鸣器响
#define BEEP_OFF  digitalWrite(BEEP_PIN, LOW)//蜂鸣器停
#define BEEP_PWM  digitalWrite(BEEP_PIN, !digitalRead(BEEP_PIN))//警报

//霍尔编码器测速采样时间为100ms
float Encoder_sample_time = 30;
unsigned long Encoder_refresh_time = 0;

//定义电机引脚-左轮
#define LEFT_PWM_1 25
#define LEFT_PWM_2 26
int freq1 = 2000;    // 频率
int channel1 = 1;    // 通道1，共16个通道，0~15
int resolution1 = 10;   // 分辨率，取值0~20，duty最大取值为2^resolution-1
int freq2 = 2000;    // 频率
int channel2 = 2;    // 通道2，共16个通道，0~15
int resolution2 = 10;   // 分辨率，取值0~20，duty最大取值为2^resolution-1
//霍尔编码器测速引脚-左轮
#define LEFT_A_PIN 13
#define LEFT_B_PIN 14
#define LEFT_A  digitalRead(LEFT_A_PIN)
#define LEFT_B  digitalRead(LEFT_B_PIN)
//电平是否变化标志位
int LEFT_flagA = 0;
int LEFT_flagB = 0;
//用来储存A相B相的脉冲数
double LEFT_valA = 0;
double LEFT_valB = 0;
//电机转速
double LEFT_n;

//定义电机引脚-右轮
#define RIGHT_PWM_1 32
#define RIGHT_PWM_2 33
int freq3 = 2000;    // 频率
int channel3 = 3;    // 通道3，共16个通道，0~15
int resolution3 = 10;   // 分辨率，取值0~20，duty最大取值为2^resolution-1
int freq4 = 2000;    // 频率
int channel4 = 4;    // 通道4，共16个通道，0~15
int resolution4 = 10;   // 分辨率，取值0~20，duty最大取值为2^resolution-1
//霍尔编码器测速引脚-右轮
#define RIGHT_A_PIN 35
#define RIGHT_B_PIN 34
#define RIGHT_A  digitalRead(RIGHT_A_PIN)
#define RIGHT_B  digitalRead(RIGHT_B_PIN)
//电平是否变化标志位
int RIGHT_flagA = 0;
int RIGHT_flagB = 0;
//用来储存A相B相的脉冲数
double RIGHT_valA = 0;
double RIGHT_valB = 0;
//电机转速
double RIGHT_n;

//mpu6050中断引脚
#define MPU6050_INT 23

//定义按键引脚
#define KEYU_PIN    16  //gpio16
#define KEYM_PIN    4   //gpio4
#define KEYD_PIN    0   //gpio0
//读取按键键值
#define KEYU  digitalRead(KEYU_PIN)
#define KEYM  digitalRead(KEYM_PIN)
#define KEYD  digitalRead(KEYD_PIN)
//枚举读出来的按键值
enum key_state{
  NO_KEY_PRES,
  KEYU_PRES,
  KEYM_PRES,
  KEYD_PRES
};

//刷新时间定义
struct TIME_SAMPLE{
  float sample_time = 100;
  unsigned long refresh_time = 0;
}MPU_time,OLED_time,Encoder_time;

//定义MPU6050数据结构体
struct MPU6050_DATE{
  //当前数据
  float AngleX = 0;
  float AngleY = 0;
  float AngleZ = 0;
  float AccX = 0;
  float AccY = 0;
  float AccZ = 0;
  float GyroX = 0;
  float GyroY = 0;
  float GyroZ = 0;
  //机械中值
  float medianY = 0;
  //初始角度
  float beginX = 0;
  float beginY = 0;
  float beginZ = 0;
  //设置极值
  float AngleX_Max = 0;
  float AngleY_Max = 0;
  float AngleZ_Max = 0;
  float GyroX_Max = 0;
  float GyroY_Max = 0;
  float GyroZ_Max = 0;
}MPU6050_NOW,MPU6050_BUFF;

//定义左轮右轮数据结构体
struct WHEEL_DATE{
  //当前时刻轮子的PWM，带正反方向
  int PWM = 0;
  //霍尔编码器测速引脚上次状态记录
  bool A_flag = 0;
  bool B_flag = 0;
  //用来储存A相B相的脉冲数
  int A_val = 0;
  int B_val = 0;
  //用来储存速度,带方向
  int speed = 0;
}LEFT,RIGHT;

//PID参数定义
struct PID_DEFINE{
  float kp = 0;
  float ki = 0;
  float kd = 0;
  //定义PID求出来的PWM
  int PWM = 0;
}TURN,VELOCITY,BALANCE;

#endif